package com.yunkaweilai.android.view.popup;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.interfaces.PopupAddMemberMoney;
import com.yunkaweilai.android.interfaces.StringInterface;
import com.yunkaweilai.android.model.PayTypeModel;
import com.yunkaweilai.android.model.TypeModel;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.ChoiceView;
import com.yunkaweilai.android.view.MyToast;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import razerdp.basepopup.BasePopupWindow;

/**
 * 开卡金额
 * Created by Administrator on 2017/7/17.
 */

public class MemberAddMoneyPopup extends BasePopupWindow{

    private View popupView;
    private Activity mContext;
    private PopupAddMemberMoney popupAddMemberMoney;
    private float money;
    private ArrayList<TypeModel>list = new ArrayList<>();
    private ArrayList<String> listName = new ArrayList<>();
    private CommonAdapter<TypeModel>adapter;
    private ListView idListView;
    private TextView idTvRight;
    private EditText idEdtNowMoney;
    private TextView idTvALL;
    private StringInterface stringInterface;
    public MemberAddMoneyPopup(Activity context, float money) {
        super(context);
        this.mContext = context;
        this.money = money;
        this.stringInterface = stringInterface;
        bindEvent();
    }
    public void setMemberMoney(PopupAddMemberMoney popupAddMemberMoney){
        this.popupAddMemberMoney = popupAddMemberMoney;
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 2, 0, 300);
    }

    @Override
    public View getClickToDismissView() {
        return popupView.findViewById(R.id.id_img_close);
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.popup_add_member_money, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.id_rlayout_bottom);
    }

    private void bindEvent() {
        initPayType();
        initAdapter();
        if (popupView != null) {
            idListView = ((ListView) popupView.findViewById(R.id.id_listview));
            idEdtNowMoney = ((EditText) popupView.findViewById(R.id.id_edt_now_money));
            idEdtNowMoney.setText(money+"");
            idListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            idTvALL = ((TextView) popupView.findViewById(R.id.id_tv_all));
            idTvALL.setText(Utils.ManageMoney(money+"") + "元");

            ListAdapter adapter = new ArrayAdapter<String>(mContext, R.layout.item_list_single_check, listName) {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    final ChoiceView view;
                    if(convertView == null) {
                        view = new ChoiceView(mContext);
                    } else {
                        view = (ChoiceView)convertView;
                    }
                    view.setText(getItem(position));
                    return view;
                }
            };
            idListView.setAdapter(adapter);
            idTvRight = ((TextView) popupView.findViewById(R.id.id_tv_right));
            initClick();
        }

    }

    private void initClick() {
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (StringUtils.isEmpty(idEdtNowMoney.getText().toString())){
//                    MyToast.showShortToast(mContext,"还未填写金额");
//                    return;
//                }
                int position = idListView.getCheckedItemPosition();
                if (position == -1){
                    MyToast.showShortToast(mContext,"还未选择支付方式");
                    return;
                }
                popupAddMemberMoney.addMoney(money+"",list.get(position).getType());
                dismiss();
            }
        });

    }

    private void initAdapter() {
        adapter = new CommonAdapter<TypeModel>(mContext,R.layout.item_list_single_check,list) {
            @Override
            protected void convert(ViewHolder viewHolder, TypeModel item, int position) {

            }
        };
    }

    private void initPayType() {
        list.clear();
        PayTypeModel payModel = BaseApplication.getPayModel();
        if (payModel != null){
            TypeModel model;
            model = new TypeModel();
            model.setName("现金支付");
            model.setType("cash");
            list.add(model);
            listName.add("现金支付");
            if ("1".equals(payModel.getData().getInfo().getIs_bank_pay())){
                model = new TypeModel();
                model.setName("银行卡支付");
                model.setType("bank");
                list.add(model);
                listName.add("银行卡支付");
            }
            if ("1".equals(payModel.getData().getInfo().getIs_wechat_pay())){
                model = new TypeModel();
                model.setName("微信支付");
                model.setType("wechat");
                list.add(model);
                listName.add("微信支付");
            }
            if ("1".equals(payModel.getData().getInfo().getIs_alipay())){
                model = new TypeModel();
                model.setName("支付宝支付");
                model.setType("alipay");
                list.add(model);
                listName.add("支付宝支付");
            }
        }
    }
}
